//
//  BVLocationUtil.m
//  BVCommon
//
//  Created by xiute on 2019/10/17.
//

#import "BVLocationUtil.h"
#import "SVProgressHUD.h"

@interface BVLocationUtil ()<CLLocationManagerDelegate>

@property (nonatomic, strong) NSMutableArray<CompleteLocation> *locationBlocks;

@property (nonatomic, strong) CLLocationManager *locationManager;

@end


@implementation BVLocationUtil

+ (instancetype)sharedInstance {
    static BVLocationUtil* LOCATION = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        LOCATION = [[BVLocationUtil alloc] init];
    });
    return LOCATION;
}

- (void)startLocationWithBlock:(CompleteLocation)block {
    [self.locationBlocks addObject:block];
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways ||
              [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self.locationManager startUpdatingLocation];
    }else {
        [SVProgressHUD showErrorWithStatus:@"请检查定位权限"];
    }
}

- (MKCoordinateRegion)regionForLocations:(NSArray<BVMetaArena *> *)locations {
    __block NSNumber *maxLongtitude = nil;
    __block NSNumber *maxLaitude = nil;
    __block NSNumber *minLongtitude = nil;
    __block NSNumber *minLaitude = nil;

    [locations enumerateObjectsUsingBlock:^(BVMetaArena * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (maxLaitude == nil) {
            maxLaitude = obj.location.latitude;
        }
        if (maxLongtitude == nil) {
            maxLongtitude = obj.location.longitude;
        }
        if (minLongtitude == nil) {
            minLongtitude = obj.location.longitude;
        }
        if (minLaitude == nil) {
            minLaitude = obj.location.latitude;
        }

        if (obj.location.latitude.doubleValue > maxLaitude.doubleValue) {
            maxLaitude = obj.location.latitude;
        }
        if (obj.location.latitude.doubleValue > maxLongtitude.doubleValue) {
            maxLongtitude = obj.location.longitude;
        }
        if (obj.location.latitude.doubleValue < minLaitude.doubleValue) {
            minLaitude = obj.location.latitude;
        }
        if (obj.location.latitude.doubleValue < minLongtitude.doubleValue) {
            minLongtitude = obj.location.longitude;
        }

    }];
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLaitude.doubleValue + maxLaitude.doubleValue) / 2.0, (minLongtitude.doubleValue + maxLongtitude.doubleValue) / 2.0);
    MKCoordinateSpan span = MKCoordinateSpanMake((maxLaitude.doubleValue - minLaitude.doubleValue) * 1.1, (maxLongtitude.doubleValue - minLongtitude.doubleValue) * 1.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    return region;
}

#pragma mark - location delegate
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
    if (locations) {
        [self.locationManager stopUpdatingLocation];
        [self.locationBlocks enumerateObjectsUsingBlock:^(CompleteLocation  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj(locations.firstObject);
        }];
        [self.locationBlocks removeAllObjects];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [self.locationBlocks enumerateObjectsUsingBlock:^(CompleteLocation  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           obj(nil);
       }];
       [self.locationBlocks removeAllObjects];
}



- (NSMutableArray<CompleteLocation> *)locationBlocks {
    if (!_locationBlocks) {
        _locationBlocks = [NSMutableArray array];
    }
    return _locationBlocks;
}

- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _locationManager;
}

@end
